
import java.sql.Connection;
import java.sql.Statement;


public class DataBaseManagement{

	private java.sql.Connection connection = null; //for holding the connection to the database
	private java.sql.Statement statement; //for executing the update or query statements

	private String databaseID = "postgres"; //holds the database login ID
	private String databasePassword = "pass123";  //holds the database login password

	private String databaseName = "tempdb";  //holds the database name for question database
	private String tableName = "temptable"; //holds the tatble name

        public Connection getConnection() {
            return connection;
        }

        public void setConnection(Connection connection) {
            this.connection = connection;
        }

        public Statement getStatement() {
            return statement;
        }

        public void setStatement(Statement statement) {
         	this.statement = statement;
        }
        
	public void init(){
		try{
			Class.forName("org.postgresql.Driver");
		}catch(ClassNotFoundException e){
			System.err.println(e.getMessage());
		}

		try{
			connection = java.sql.DriverManager.getConnection("jdbc:postgresql://localhost/"+databaseName , databaseID, databasePassword);
			statement = connection.createStatement();
		
		}catch(java.sql.SQLException e){
			System.err.println(e.getMessage());
		}
	}

	
	public void create(){

		try{
			Class.forName("org.postgresql.Driver");
		}catch(ClassNotFoundException e){
			System.err.println(e.getMessage());
		}

		try{
			connection = java.sql.DriverManager.getConnection("jdbc:postgresql://localhost/", databaseID, databasePassword);
			statement = connection.createStatement();
			statement.executeUpdate("DROP DATABASE "+databaseName);
			statement.executeUpdate("CREATE DATABASE "+databaseName);
			connection = java.sql.DriverManager.getConnection("jdbc:postgresql://localhost/"+databaseName , databaseID, databasePassword);
			statement = connection.createStatement();
			statement.executeUpdate("CREATE TABLE "+tableName+" ( question varchar(255), questionType int,answer varchar(255), choice1 varchar(255), choice2 varchar(255), choice3 varchar(255))");

		}catch(java.sql.SQLException e){
			System.err.println(e.getMessage());
		}
	}

	public void writeQuestionToDataBase(String[] attributes){
		try{
			statement.executeUpdate("INSERT INTO "+tableName+" VALUES( '"+attributes[0]+"', "+Integer.parseInt(attributes[1])+", '"+attributes[2]+"', '"+attributes[3]+"', '"+attributes[4]+"', '"+attributes[5]+"')");

		}catch(java.sql.SQLException e){
			System.err.println(e.getMessage());
		}
	}

	public java.util.ArrayList<Question> readQuestinoFromDataBase(){
		java.util.ArrayList<Question> questionList = new java.util.ArrayList<Question>();
		try{
		java.sql.ResultSet resultSet = statement.executeQuery("SELECT * FROM "+tableName);
		while(resultSet.next()){
			if(Integer.parseInt(resultSet.getString("questionType"))==1){
				ImageChoice[] imageChoices = new ImageChoice[4];
				imageChoices[0] = new ImageChoice(resultSet.getString("Answer"));
				imageChoices[0].setAnswer(true);

				imageChoices[1] = new ImageChoice(resultSet.getString("choice1"));
				imageChoices[2] = new ImageChoice(resultSet.getString("choice2"));
				imageChoices[3] = new ImageChoice(resultSet.getString("choice3"));
				Question question = new Question(resultSet.getString("question"),imageChoices);
				question.setImageQuestion(true);
				questionList.add(question);
			}else if(Integer.parseInt(resultSet.getString("questionType"))==0){
				Choice[] choices = new Choice[4];
				choices[0] = new Choice(resultSet.getString("Answer"));
				choices[0].setAnswer(true);

				choices[1] = new Choice(resultSet.getString("choice1"));
				choices[2] = new Choice(resultSet.getString("choice2"));
				choices[3] = new Choice(resultSet.getString("choice3"));
				Question question = new Question(resultSet.getString("question"),choices);
				//question.setImageQuestion(false);
				questionList.add(question);
			}
		}
		}catch(java.sql.SQLException e){
			System.err.println(e.getMessage());
		}
		return questionList;
	}

	public java.util.ArrayList<Question> readQuestinoFromDataBase(int numberOfQuestion){
		java.util.ArrayList<Question> questionList = new java.util.ArrayList<Question>();
		int count = 0;
		try{
		java.sql.ResultSet resultSet = statement.executeQuery("SELECT * FROM "+tableName);
		while(count++<numberOfQuestion && resultSet.next()){
			if(Integer.parseInt(resultSet.getString("questionType"))==1){
				ImageChoice[] imageChoices = new ImageChoice[4];
				imageChoices[0] = new ImageChoice(resultSet.getString("Answer"));
				imageChoices[0].setAnswer(true);

				imageChoices[1] = new ImageChoice(resultSet.getString("choice1"));
				imageChoices[2] = new ImageChoice(resultSet.getString("choice2"));
				imageChoices[3] = new ImageChoice(resultSet.getString("choice3"));
				Question question = new Question(resultSet.getString("question"),imageChoices);
				question.setImageQuestion(true);
				questionList.add(question);
			}else if(Integer.parseInt(resultSet.getString("questionType"))==0){
				Choice[] choices = new Choice[4];
				choices[0] = new Choice(resultSet.getString("Answer"));
				choices[0].setAnswer(true);

				choices[1] = new Choice(resultSet.getString("choice1"));
				choices[2] = new Choice(resultSet.getString("choice2"));
				choices[3] = new Choice(resultSet.getString("choice3"));
				Question question = new Question(resultSet.getString("question"),choices);
				//question.setImageQuestion(false);
				questionList.add(question);
			}
		}
		}catch(java.sql.SQLException e){
			System.err.println(e.getMessage());
		}
		return questionList;
	}

/*
	public static void main(String para[]){	
		DataBaseManagement app = new DataBaseManagement();
	}
*/
}